using System;
using AndyPike.Presentations.IntroToTDD.Interfaces;

namespace AndyPike.Presentations.IntroToTDD
{
    public class StringReverserUsingIndexSwapping : IReverser<string>
    {
        private readonly ILogger logger;

        public StringReverserUsingIndexSwapping(ILogger logger)
        {
            if (logger == null)
                throw new ArgumentNullException("logger");

            this.logger = logger;
        }

        public string Reverse(string input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            char temp = '\0';
            int index = 0;
            char[] chars = input.ToCharArray();
            for (int x = 0; x < input.Length / 2; x++ )
            {
                index = input.Length - 1 - x;
                temp = chars[x];
                chars[x] = chars[index];
                chars[index] = temp;
            }

            var output = new string(chars);
            
            logger.Info(string.Format("The string '{0}' was reversed to '{1}'.", input, output));

            return output;
        }
    }
}